﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：汪定新
// Email: 
// 日 期：2012/5/16
// 描 述：人事异动联动更新其他表数据
// 版 本：1.0.0.0    
//    
//==================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using ZX.DBModule;
using ZX.EHR.Library;

namespace ZX.BLL
{
    public class EmployeeChangesJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            string condition = string.Empty;
            BaseEntity instance = null;
            ZX.BLL.Extend.BaseBussnies bll = null;
            IList<BaseEntity> list = null;
            BaseEntity hrEmployeeInstance = ZX.Common.Globals.getEntity("hrEmployee") as BaseEntity;
            ZX.BLL.Extend.BaseBussnies hrEmployeeBll = new ZX.BLL.Extend.BaseBussnies(hrEmployeeInstance);
            try
            {
                //岗位异动后要更新员工信息表中的部门，职务信息
                //获取岗位异动表中当前日期>=异动日期的记录
                condition = string.Format("ValidDate<='{0}'and AvilableStatus=3 and PostType=0", DateTime.Now.ToString("yyyy-MM-dd"));
                instance = ZX.Common.Globals.getEntity("hrEmployeePositions") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(instance);

                list = bll.GetEntitysByCondition(condition);//获取所有符合条件的记录
                if (list != null)
                {
                    foreach (BaseEntity entity in list)
                    {
                        hrEmployeePositions hremployeePositions = entity as hrEmployeePositions;
                        condition = string.Format("EmployeeID='{0}' and AvilableStatus=2 and PostType=0", hremployeePositions.EmployeeID.ToString());
                        IList<BaseEntity> list2 = bll.GetEntitysByCondition(condition);//获取目前正在就职的岗位
                        if (list2 != null)
                        {
                            for (int i = 0; i < list2.Count; i++)
                            {
                                hrEmployeePositions tempHrEmployeePositions = list2[i] as hrEmployeePositions;
                                tempHrEmployeePositions.AvilableStatus = 1;//将之前正在就职的岗位状态更新为过期
                                bll = new ZX.BLL.Extend.BaseBussnies(tempHrEmployeePositions);
                                bll.Update();
                            }
                        }
                        hremployeePositions.AvilableStatus = 2;//更新岗位状态为正在就职
                        bll = new ZX.BLL.Extend.BaseBussnies(hremployeePositions);
                        bll.Update();
                        hrEmployee hremployee = hrEmployeeBll.GetEntityById(hremployeePositions.EmployeeID.ToString()) as hrEmployee;
                        hremployee.PostID = hremployeePositions.PostID;//更新岗位   
                        hremployee.DepartmentID = hremployeePositions.DeptID; //更新员工部门
                        hrEmployeeBll.Update(hremployee);//更新到数据库}
                    }
                }
            }
            catch (Exception e)
            { }

            try
            {
                //薪资异动后要薪资表中的薪资状态信息
                instance = ZX.Common.Globals.getEntity("hrEmployeeSalary") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(instance);
                condition = string.Format("ValidDate<='{0}'and AvilableStatus=3", DateTime.Now.ToString("yyyy-MM-dd"));//生效日期到达并且是即将使用的状态
                list = bll.GetEntitysByCondition(condition);//获取所有符合条件的记录
                if (list != null)
                {
                    foreach (BaseEntity entity in list)
                    {
                        hrEmployeeSalary hremployeeSalary = entity as hrEmployeeSalary;
                        condition = string.Format("EmployeeID='{0}' and AvilableStatus=2", hremployeeSalary.EmployeeID.ToString());
                        IList<BaseEntity> list2 = bll.GetEntitysByCondition(condition);//获取目前正在使用的薪资记录
                        if (list2 != null)
                        {
                            for (int i = 0; i < list2.Count; i++)
                            {
                                hrEmployeeSalary tempHremployeeSalary = list2[i] as hrEmployeeSalary;
                                tempHremployeeSalary.AvilableStatus = 1;//将之前正在使用的薪资状态更新为过期
                                bll = new ZX.BLL.Extend.BaseBussnies(tempHremployeeSalary);
                                bll.Update();
                            }
                        }
                        hremployeeSalary.AvilableStatus = 2;
                        bll = new ZX.BLL.Extend.BaseBussnies(hremployeeSalary);//更新薪资信息
                        bll.Update();
                    }
                }
            }
            catch
            { }

            try
            {
                //人事事务处理
                instance = ZX.Common.Globals.getEntity("hrEmployeeAffairs") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(instance);
                condition = string.Format(" EffectiveDate<='{0}' and flag=0", DateTime.Now.ToString("yyyy-MM-dd"));//条件为到达生效日期,并且异动类型为离职
                list = bll.GetEntitysByCondition(condition);//获取所有符合条件的记录
                if (list != null)
                {
                    foreach (BaseEntity entity in list)
                    {
                        hrEmployeeAffairs hrEmpAffairs = entity as hrEmployeeAffairs;
                        byte empStatus = 1;//员工状态(1.在职；2.不在职，3.离职，4.离退休)
                        bool flag = false;//更新是否停用账户
                        //事务类型（1.在职转不在职，2.不在职转在职,3.辞职,4.辞退,5.离退休,6.返聘,7.解返聘,8.转正）
                        switch (hrEmpAffairs.AffairsType) //人事事务
                        {
                            case 1://在职转不在职
                                empStatus = 2;
                                flag = true;
                                break;
                            case 2://不在职转在职
                            case 6://返聘
                                empStatus = 1;
                                flag = false;
                                break;
                            case 3:
                            case 4:
                                empStatus = 3;
                                flag = false;
                                break;
                            case 5:
                            case 7:
                                empStatus = 4;
                                flag = false;
                                break;
                            case 8:
                                empStatus = 254;//转正
                                break;
                        }
                        hrEmployee hremployee = hrEmployeeBll.GetEntityById(hrEmpAffairs.EmployeeID.ToString()) as hrEmployee;
                        if (empStatus == 254)//转正
                        {
                            hremployee.EmployeeType = new Guid("D924C147-1A23-4FBE-A958-2F58FD9377D7");
                        }
                        else
                        {
                            hremployee.EmployeeStatus = empStatus;//修改员工状态
                        }
                        hrEmployeeBll.Update(hremployee);
                        try
                        {
                            hrEmpAffairs.flag = 1;
                            bll.Update(hrEmpAffairs);
                        }
                        catch { }
                        try
                        {
                            //更新用户表
                            condition = string.Format("EmpID='{0}'", hrEmpAffairs.EmployeeID.ToString());
                            instance = ZX.Common.Globals.getEntity("DatUser") as BaseEntity;
                            ZX.BLL.Extend.BaseBussnies DatUserBll = new ZX.BLL.Extend.BaseBussnies(instance);
                            DatUser datUser = DatUserBll.GetEntityByCondition(condition) as DatUser;//获取该员工用户信息
                            datUser.ExpireDate = DateTime.Now;
                            datUser.AddUser = Guid.Empty;
                            datUser.IsExpire = flag;
                            DatUserBll.Update(datUser);//更新员工用户表
                        }
                        catch
                        { }
                    }
                }
            }
            catch
            { }

            //兼职岗位生效
            try
            {
                //获取岗位表中当前日期>=异动日期的记录
                condition = string.Format("ValidDate<='{0}'and AvilableStatus=3 and PostType=1", DateTime.Now.ToString("yyyy-MM-dd"));
                instance = ZX.Common.Globals.getEntity("hrEmployeePositions") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(instance);
                list = bll.GetEntitysByCondition(condition);//获取所有符合条件的记录
                if (list != null)
                {
                    foreach (BaseEntity entity in list)
                    {
                        hrEmployeePositions hremployeePositions = entity as hrEmployeePositions;
                        hremployeePositions.AvilableStatus = 2;//更新岗位状态为正在就职
                        bll = new ZX.BLL.Extend.BaseBussnies(hremployeePositions);
                        bll.Update();
                    }
                }
            }
            catch
            { }
        }
    }
}
